package com.qf.dao.impl;

import com.qf.dao.GoodsDao;
import com.qf.pojo.TbGoods;
import com.qf.utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/8/16 17:42
 */
public class GoodsDaoImpl implements GoodsDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtils.getDataSource());
    @Override
    public List<TbGoods> selectByTypeId(int typeId) {

        String sql="select * from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=?";
        try {
            return queryRunner.query(sql, new BeanListHandler<>(TbGoods.class),typeId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Long selectCount(long id) {
        String sql="select count(0) from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=?";
        try {
            return queryRunner.query(sql, new ScalarHandler<>(),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public List<TbGoods> selectList(long id, int currentPage, int pageSize) {
        String sql="select * from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.typeid=? limit ?,?";
        Object[] param={id,(currentPage-1)*pageSize,pageSize};
        try {
            return queryRunner.query(sql, new BeanListHandler<>(TbGoods.class),param);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TbGoods selectById(long id) {
        String sql="select * from tb_goods_kz where id=?";
        try {
            return queryRunner.query(sql, new BeanHandler<>(TbGoods.class),id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public TbGoods findById(long pid) {
//        String sql="select * from tb_goods tg,tb_goods_kz tgz where tg.kzid=tgz.id and tgz.id=?";
        String sql="select * from tb_goods_kz where id=?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(TbGoods.class),pid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<TbGoods> selectAll() {
        String sql="select * from tb_goods_kz";
        try {
            return queryRunner.query(sql, new BeanListHandler<>(TbGoods.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void deleteGoods(long id) {
        String sql="delete from tb_goods_kz where id=?";
        try {
            queryRunner.update(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateGoods(TbGoods goods) {
        String sql="update tb_goods_kz set name=?,picture=?,price=?,intro=?,typeid=? where id=?";
        Object[] param={goods.getName(),goods.getPicture(),goods.getPrice(),goods.getIntro(),goods.getTypeid(),goods.getId()};
        try {
            queryRunner.update(sql,param);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void insertGoods(TbGoods goods) {
        String sql="insert into tb_goods_kz (name,picture,price,star,intro,typeid) values (?,?,?,?,?,?)";
        Object[] param={goods.getName(),goods.getPicture(),goods.getPrice(),goods.getStar(),goods.getIntro(),goods.getTypeid()};
        try {
            queryRunner.update(sql,param);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
